<?php
namespace app\admin\controller;
use app\admin\model\ProductRule as ProductRuleModel;
use app\common\BaseController;

class ProductRule extends BaseController {

	/**
	 * [index 首页列表]
	 * @return [type] [description]
	 */
	public  function index(){
        $page  = (int)request()->param('page', 0);
        $limit  = (int)request()->param('limit', 10);
        $search = request()->param();
        $wheres = [];
        if(isset($search['title']) && $search['title'] !=''){
            $wheres[] = ['title','like',"%{$search['title']}%"];
        }
        $query  = ProductRuleModel::where($wheres);
        $total  = $query->count();
        $data   = $query->page($page, $limit)->order('id desc')->select();
        if(count($data)){
            foreach($data as $k=>$item){
                if ($item['rule_value']) {
                    $item['specs'] = json_decode($item['rule_value'], true);
                    $item['attr_name'] = array_column($item['specs'], 'value');
                    $item['attr_value'] = array_column($item['specs'], 'detail');
                }
            }
        }
        return $this->success('', [
            'start' => $page,
            'limit' => $limit,
            'data'  => $data,
            'total' => $total,
        ]);
	}

	/**
	 * 添加商品规则
	 */
	public function create($id=0){
        $data = request()->post();
        try {
            $this->validate($data, [
                'rule_name' => 'require',
                'rule_value' => 'require',
            ], [
               'rule_name.require'  => '请输入规则名称',
               'rule_value.require' => '缺少规则值',
            ]);
        } catch (\Exception $e) {
            json_response(0,$e->getError());
        }
        $data['rule_name'] = request()->post('rule_name','');
        $data['rule_value'] = request()->post('rule_value/a',[]);
        $data['rule_value'] = json_encode($data['rule_value']);
        if (ProductRuleModel::create($data)) json_response(1,'规则添加成功!');;
        json_response(0,'规则添加失败!');
	}

        /**
     * 详情
     * @param int $id
     * @return \think\response\Json
     * @author Sam
     * @date 2022-05-06
     */
    public function detail(int $id): \think\response\Json
    {
        try {
            $row  = ProductRuleModel::where('id', $id)->find();
            $row->rule_value = json_decode($row->rule_value, true);
        } catch (DataNotFoundException | ModelNotFoundException | DbException $e) {
            return $this->error($e->getMessage());
        }

        return $this->success('', $row);
    }
	/**
	 * 编辑商品规则
	 * @param  integer $id [description]
	 * @return [type]      [description]
	 */
	public function edit(){
        $data = request()->post();
        try {
            $this->validate($data, [
                'id' => 'require',
                'rule_name' => 'require',
                'rule_value' => 'require',
            ], [
                'id.require'  => '缺少规则ID',
               'rule_name.require'  => '请输入规则名称',
               'rule_value.require' => '缺少规则值',
            ]);
        } catch (\Exception $e) {
            json_response(0,$e->getError());
        }
		$id = param_check('id');
		if(request()->isPost()) {
	        $data['rule_name'] = request()->post('rule_name','');
	        $data['rule_value'] = request()->post('rule_value/a',[]);
	        $data['rule_value'] = json_encode($data['rule_value']);
	         $rule = ProductRuleModel::find($id);
	          if (!$rule) json_response(0,'数据不存在');
	            ProductRuleModel::update($data,['id'=>$id]);
	            json_response(1,'编辑成功!');
         }else{
         	 $this->assign(compact('id'));
            return $this->fetch();
         }
	}

	    /**
     * 显示指定的资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function read($id)
    {
        return json_response(1,'success',ProductRuleModel::sysInfo($id));
    }


    /**
     * 删除
     * @return void
     * @author Sam
     * date 2021-08-16
     */
    public function del(){
        $id = param_check('id');
        if(!$rule = ProductRuleModel::get($id)) json_response(0,'规则不存在');
        if($rule->delete()) json_response(1,'删除成功');
        json_reponse(0,'删除失败');
    }
}
